/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import java.util.ArrayList;

/**
 *
 * @author Renrhaf
 */
public class Trinucleotide {
    /** Possible letters for the trinucleotide word **/
    public static final char[] letters = {'A', 'C', 'G', 'T'};
    
    /** This trinucleotide word **/
    private String word;
    
    /**
     * Creates a new trinucleotide.
     * @param word the three letters of the trinucleotide
     * @throws Exception if the letters or the length is not correct
     */
    public Trinucleotide(String word) /*throws Exception*/ {
        // Checks the length
        if(word.length() == 3) {
            // Checks if the letters are correct
            for(int i = 0; i < 3; i++) {
                boolean found = false;
                for(int j = 0; j < letters.length; j++) {
                    if(word.charAt(i) == letters[j]) {
                        found = true; break;
                    }
                }
                if(!found) {
                    //throw new Exception("Erreur dans les lettres du trinucléotide : "+word);
                }
            }
            this.word = word;
        }
    }
    
    public String getWord() {
        return this.word;
    }
    
    /**
     * Returns the mirror of the trinucleotide.
     * @return 
     */
    public String getMirror() {
        return ("" + this.word.charAt(2) + this.word.charAt(1) + this.word.charAt(0));
    }
    
    /**
     * Returns the mirror of the trinucleotide.
     * @return 
     */
    public ArrayList<String> getConjugates() {
        ArrayList<String> res = new ArrayList<String>();
        res.add("" + this.word.charAt(1) + this.word.charAt(2) + this.word.charAt(0));
        res.add("" + this.word.charAt(2) + this.word.charAt(1) + this.word.charAt(0));
        return res;
    }

    public String getComplementary() {
        String res = "";
        
        for(int i=this.word.length()-1; i>=0 ; i--) {
        	//On parcourt le mot dans le sens inverse, afin de placer directement les lettres complémentaires dans le bon ordre
            if(this.word.charAt(i) == 'A') { res += 'T'; }
            else if(this.word.charAt(i) == 'C') { res += 'G'; }
            else if(this.word.charAt(i) == 'G') { res += 'C'; }
            else if(this.word.charAt(i) == 'T') { res += 'A'; }
        }
        
        return res;        
    }
    
    public String getPermuted() {
        return ("" + this.word.charAt(1) + this.word.charAt(2) + this.word.charAt(0));          
    }
    
    public static ArrayList<Trinucleotide> getY() {
        ArrayList<Trinucleotide> liste = new ArrayList<Trinucleotide>();
        liste.add(new Trinucleotide(""+letters[0]+letters[0]+letters[1]));
        liste.add(new Trinucleotide(""+letters[0]+letters[0]+letters[3]));
        liste.add(new Trinucleotide(""+letters[0]+letters[1]+letters[1]));
        liste.add(new Trinucleotide(""+letters[0]+letters[3]+letters[1]));
        liste.add(new Trinucleotide(""+letters[0]+letters[3]+letters[3]));
        liste.add(new Trinucleotide(""+letters[1]+letters[0]+letters[2]));
        liste.add(new Trinucleotide(""+letters[1]+letters[3]+letters[1]));
        liste.add(new Trinucleotide(""+letters[1]+letters[3]+letters[2]));
        liste.add(new Trinucleotide(""+letters[2]+letters[0]+letters[0]));
        liste.add(new Trinucleotide(""+letters[2]+letters[0]+letters[1]));
        liste.add(new Trinucleotide(""+letters[2]+letters[0]+letters[2]));
        liste.add(new Trinucleotide(""+letters[2]+letters[0]+letters[3]));
        liste.add(new Trinucleotide(""+letters[2]+letters[1]+letters[1]));
        liste.add(new Trinucleotide(""+letters[2]+letters[2]+letters[1]));
        liste.add(new Trinucleotide(""+letters[2]+letters[2]+letters[3]));
        liste.add(new Trinucleotide(""+letters[2]+letters[3]+letters[0]));
        liste.add(new Trinucleotide(""+letters[2]+letters[3]+letters[1]));
        liste.add(new Trinucleotide(""+letters[2]+letters[3]+letters[3]));
        liste.add(new Trinucleotide(""+letters[3]+letters[0]+letters[1]));
        liste.add(new Trinucleotide(""+letters[3]+letters[3]+letters[1]));
        return liste;
    }
    
    
    public static ArrayList<Trinucleotide> getY1() {
        ArrayList<Trinucleotide> liste = Trinucleotide.getY();
        for(int i = 0; i < liste.size(); i++) {
            liste.set(i, new Trinucleotide(liste.get(i).getPermuted()));
        }
        return liste;
    }
    
    public static ArrayList<Trinucleotide> getY2() {
        ArrayList<Trinucleotide> liste = Trinucleotide.getY1();
        for(int i = 0; i < liste.size(); i++) {
            liste.set(i, new Trinucleotide(liste.get(i).getPermuted()));
        }
        return liste;
    }
    
    public String toString() {
        return " "+this.word+" ";
    }
}
